package com.enniswu.weitter.moment.post;

import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.enniswu.weitter.base.BasePresenter;
import com.enniswu.weitter.data.remote.FileLoader;
import com.enniswu.weitter.data.remote.PostLoader;

import java.io.File;
import java.util.List;

public class PostPresenter extends BasePresenter<PostContract.View> implements PostContract.Presenter {

    private int type;
    private PostLoader pLoader;
    private FileLoader fLoader;

    public PostPresenter(int type, PostContract.View view) {
        super(view);
        this.type = type;
        pLoader = new PostLoader();
        fLoader = new FileLoader();
    }

    @Override
    public void post(int id, String contend, List<File> album) {
        if (TextUtils.isEmpty(contend) && album.isEmpty()) {
            view.next(0);
            return;
        }
        uploadAlbum(id, contend, album);
    }

    private void uploadAlbum(int id, final String contend, List<File> album) {
        view.showLoading();
        if (album.isEmpty()) {
            post(contend);
            return;
        }
        fLoader.uploadAlbum(id, album, new AbstractCallback<Object>() {
            @Override
            public void onSuccess(@Nullable Object o) {
                post(contend);
            }
        });
    }

    private void post(String contend) {
        pLoader.post(contend, type, 0, new AbstractCallback<Integer>() {
            @Override
            public void onSuccess(@Nullable Integer o) {
                view.dismissLoading();
                view.next(o);
            }
        });
    }
}
